//
//  QCTableViewController.m
//  YaoHeAPP
//
//  Created by xyh on 15/12/16.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "QCTableViewController.h"
#import "Controller.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
@interface QCTableViewController ()

{  //图片数组
    NSMutableArray *qimagearray;
    //名称数组
    NSMutableArray *qnamearray;
    
    //小结
    NSString *zongjie;
    
    UILabel *label;
    UILabel *myLable;
    CGRect rect;


}

@end

@implementation QCTableViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"QCTableViewController"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"QCTableViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden=YES;
    
    //加一个导航条
    UINavigationBar*navcBar=[[UINavigationBar alloc]init];
    [navcBar setBackgroundImage:[UIImage imageNamed:@"bg_user_center"] forBarMetrics:UIBarMetricsDefault];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(0);
        make.height.offset(60);
        make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"质检报告";
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(30);
        make.height.offset(28);
        make.bottom.offset(-6);
    }];
    

    
    [self updata];
    
    qimagearray=[NSMutableArray array];
    qnamearray=[NSMutableArray array];
    
    
////             WithObjects:@"材质：哈哈哈",@"规格：呵呵呵呵",@"公差：uiuiuiui",@"包装：就佛得角死奥", nil];

//    text = @"小计：本店于十一期间特推出一系列优惠，限时限量敬请选购！沙发：钻石品质，首领风范！床垫：华贵典雅，彰显时尚！尊贵而不失奢华，典雅却不失自然！温馨和浪漫的生活，我们与你一同创造！";
    
    
    self.tableView.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];

}

-(void)backLogin
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//请求数据
-(void)updata
{
    
    NSString *ido=self.orderid;
    
    NSDictionary*di=@{@"ORDER_ID":ido};
    
    [Controller requestWithURL:POSTLOADQUALITY jsondic:di finishBlock:^(ClassData* data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            //图片数组
            qimagearray=data.data[@"IMG_LIST"];
            //质检项数组
            qnamearray=data.data[@"ITEM_LIST"];
            //总结
            zongjie=data.data[@"INSPECTION_REMARK"];
            
            [self.tableView reloadData];
        }
        else
        {
            
        }
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return qnamearray.count;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *GroupedTableIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             
                             GroupedTableIdentifier];
    UILabel *ll;
    UILabel  *llname;
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]
                
                initWithStyle:UITableViewCellStyleDefault
                
                reuseIdentifier:GroupedTableIdentifier];
       
        
        ll=[[UILabel alloc]init];
        ll.backgroundColor=[UIColor whiteColor];
        ll.font=[UIFont systemFontOfSize:14];
        [cell addSubview:ll];
        [ll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);  make.width.offset(80);
            make.top.offset(0);  make.bottom.offset(0);
        }];
        
        llname=[[UILabel alloc]init];
        llname.backgroundColor=[UIColor whiteColor];
        llname.font=[UIFont systemFontOfSize:14];
        llname.numberOfLines=0;
        [cell addSubview:llname];
        [llname mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(ll.mas_right).offset(3);  make.top.offset(5);
            make.bottom.offset(-5);  make.right.offset(-5);
        }];
        
    }
    
    NSString *llt=[NSString stringWithFormat:@"%@:",qnamearray[indexPath.row][@"ITEM_NAME"]];
    ll.text=llt;
    llname.text=qnamearray[indexPath.row][@"REMARK"];
     cell.backgroundColor=[UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (10+[qnamearray[indexPath.row][@"REMARK"] boundingRectWithSize: CGSizeMake(self.view.frame.size.width-100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14] ,NSFontAttributeName, nil] context:nil].size.height>40) {
        return 20+[qnamearray[indexPath.row][@"REMARK"] boundingRectWithSize: CGSizeMake(self.view.frame.size.width-100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14] ,NSFontAttributeName, nil] context:nil].size.height;
    }
    else{
        return 40;
    }
    

}

//段头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        UIView *he=[[UIView alloc]init];
        
        he.backgroundColor=[UIColor whiteColor];
        UILabel *la=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
        la.text=@"现场图片：";
        
       
        NSInteger jianxi=(self.view.frame.size.width-55*5)/6;
        for (NSInteger i=0; i<5; i++) {
            UIImageView *imGview=[[UIImageView alloc]init];
            imGview.tag=600+i;
            imGview.frame=CGRectMake(jianxi+(jianxi+55)*i, 40, 55, 55);
            
            [he addSubview:imGview];
        }
        for (NSInteger j=0; j<qimagearray.count; j++) {
            UIImageView *find_label = (UIImageView *)[he viewWithTag:600+j];
            [find_label sd_setImageWithURL:[NSURL URLWithString:qimagearray[j]] placeholderImage:[UIImage imageNamed:@"ic_qiugou_quotation"]];
        }

        [he addSubview:la];
        
        return he;
    }
    
    return nil;
}
//返回分区区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0)
    {
        return 0;
    }
    return 100;
}
//尾部
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (section==0)
    {
        return 0;
    }
    
    return 40+[zongjie boundingRectWithSize: CGSizeMake(self.view.frame.size.width-65, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15] ,NSFontAttributeName, nil] context:nil].size.height;
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    if (section==1) {
        UIView *he=[[UIView alloc]init];
        
        he.backgroundColor=[UIColor whiteColor];
        UILabel*title=[[UILabel alloc]init];
        title.frame=CGRectMake(10, 20, 45, 20);
        title.text=@"小结:";
        title.font=[UIFont systemFontOfSize:15];
        [he addSubview:title];
        
        myLable=[[UILabel alloc] init];
        myLable.font=[UIFont systemFontOfSize:15];
        myLable.numberOfLines=0;
        [myLable setBackgroundColor:[UIColor clearColor]];
        myLable.text=zongjie;
        [he addSubview:myLable];
        [myLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(title.mas_right).offset(2);
            make.top.offset(10);  make.bottom.offset(-10);
            make.right.offset(-5);
        }];
        return he;
        
    }
    
    return nil;
 
}

-(NSInteger)stringRect:(NSString*)str
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:[[NSDictionary alloc] initWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil] context:nil];
    return rect.size.width+10;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
